-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add unit tests #22
Conversation
570f000
to
d4f64c1
Compare
b7dd79f
to
18953c8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
I would like to propose that instead of running the test container in the github action, it's started by the test setup system, using something like testcontainers. This reduces a hurdle in running the tests locally.
The following diff implements this:
diff --git a/src/doctor/rules/compression_test.py b/src/doctor/rules/compression_test.py
index b74d4a7..9b8f381 100644
--- a/src/doctor/rules/compression_test.py
+++ b/src/doctor/rules/compression_test.py
@@ -19,6 +19,7 @@ import unittest
import psycopg2
from psycopg2.extras import RealDictCursor
+from testcontainers.postgres import PostgresContainer
from timescaledb import Hypertable
from doctor.rules.compression import LinearSegmentBy, PointlessSegmentBy
@@ -34,15 +35,10 @@ class TestCompressionRules(unittest.TestCase):
def setUp(self):
"""Set up unit tests for compression rules."""
- user = os.getenv("PGUSER")
- host = os.getenv("PGHOST")
- port = os.getenv("PGPORT") or "5432"
- dbname = os.getenv("PGDATABASE")
- password = os.getenv("PGPASSWORD")
- print(f"connecting to {host}:{port} database {dbname}")
- self.__conn = psycopg2.connect(dbname=dbname, user=user, host=host,
- password=password, port=port,
- cursor_factory=RealDictCursor)
+ self.__container = PostgresContainer("timescale/timescaledb-ha:pg15", driver="default").start()
+ connstring = self.__container.get_connection_url().replace("+psycopg2", "")
+ print(f"connecting to {connstring}")
+ self.__conn = psycopg2.connect(connstring, cursor_factory=RealDictCursor)
table = Hypertable("conditions", "time", {
'time': "timestamptz not null",
'device_id': "integer",
@@ -71,6 +67,7 @@ class TestCompressionRules(unittest.TestCase):
with self.__conn.cursor() as cursor:
cursor.execute("DROP TABLE conditions")
self.__conn.commit()
+ self.__container.stop()
def run_rule(self, rule):
"""Run rule and return messages."""
Seems reasonable to do, but then it might be useful to allow each test to pick the container. Some of the rules only works if you have a container that do not have TimescaleDB installed, so to check those, you need to pick a standard PostgreSQL image. Tempted to push this and handle that as a separate PR, but going to investigate if this is straightforward to do and include in this PR. |
Add unit tests and unit test framework for existing functionality.
18953c8
to
42674a6
Compare
Creating a good container support for test cases was more work then expected. Pushing this and taking this as a separate PR. |
Add unit tests and unit test framework for existing functionality.